package com.lf.web.action.system;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

public class SystemInfo extends ActionSupport implements ServletResponseAware {

    /**
     *
     */
    private static final long serialVersionUID = -2167175897976396856L;
    private static final Logger LOGGER = Logger.getLogger(SystemInfo.class);
    private HttpServletResponse res;
    private String name = System.getProperty("os.name");

    public void RestartTomcat() throws IOException {
        JSONObject json = new JSONObject();
        if (name.toLowerCase().indexOf("linux") >= 0) {
            LOGGER.info("执行重启服务");
            executeCommond("S tomcat8 restart");
            json.put("code", 1);
            json.put("msg", "操作完成 !");
        } else {
            json.put("code", -1);
            json.put("msg", "本系统不支持此操作！");
        }
        res.getWriter().write(json.toString());
    }

    private void executeCommond(String cmd) {
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            LOGGER.info("errorinfo:");
            printinfo(p.getErrorStream());
            LOGGER.info("outinf:");
            printinfo(p.getInputStream());
            p.destroy();
        } catch (IOException e) {
            LOGGER.info("命令执行异常[" + cmd + "]：" + e.getMessage());
        }
    }

    private void printinfo(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                LOGGER.info(line);
            }
        } catch (IOException e) {
            LOGGER.info("信息读取出错：" + e.getMessage());
        }
    }

    public Map<String, Object> getOs() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", System.getProperty("os.name"));
        map.put("arch", System.getProperty("os.arch"));
        map.put("version", System.getProperty("os.version"));
        return map;
    }

    public Map<String, Object> getJava() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("javaversion", System.getProperty("java.version"));
        map.put("javavendor", System.getProperty("java.vendor"));
        map.put("vmspecificationversion",
                System.getProperty("java.vm.specification.version"));
        map.put("vmspecificationvendor",
                System.getProperty("java.vm.specification.vendor"));
        map.put("vmspecificationname",
                System.getProperty("java.vm.specification.name"));
        map.put("specificationversion",
                System.getProperty("java.specification.version"));
        map.put("specificationvendor",
                System.getProperty("java.specification.vendor"));
        map.put("specificationname",
                System.getProperty("java.specification.name"));
        map.put("classversion", System.getProperty("java.class.version"));

        return map;
    }

    public Map<String, Object> getTomcat() {
        Map<String, Object> map = new HashMap<String, Object>();
        Runtime runtime = Runtime.getRuntime();
        map.put("freeMemory", runtime.freeMemory());
        map.put("totalMemory", runtime.totalMemory());
        map.put("maxMemory", runtime.maxMemory());
        return map;
    }

    @Override
    public void setServletResponse(HttpServletResponse res) {
        this.res = res;
    }
}
